<template>
    <section>
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">

                <el-form-item>
                    <el-input v-model="query.keywords" placeholder="店铺名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select clearable v-model="query.state" placeholder="店铺状态">
                        <el-option label="待审核" value="0"></el-option>
                        <el-option label="通过" value="1"></el-option>
                        <el-option label="驳回" value="-1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>



        <!--列表-->
        <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="店铺名称" width="120">
            </el-table-column>
            <el-table-column prop="tel" label="公司电话" width="100">
            </el-table-column>
            <el-table-column prop="registerTime" label="注册时间" width="100">
            </el-table-column>
            <el-table-column prop="state" label="状态" width="120">
                <template scope="scope">
                    <span v-if="scope.row.state==0" style="color: #8c939d">待审核</span>
                    <span v-if="scope.row.state==1" style="color: #13ce66">审核通过</span>
                    <span v-if="scope.row.state==-1" style="color: #ff4d51">驳回</span>
                </template>

            </el-table-column>
            <el-table-column prop="address" label="地址" min-width="180">
            </el-table-column>
            <el-table-column prop="logo" label="标志" min-width="180" >
            </el-table-column>
            <el-table-column prop="admin.username" label="管理员" min-width="180">
            </el-table-column>
            <el-table-column label="操作" width="250">
                <template scope="scope">
                    <el-button type="warning" size="small" @click="check(scope.$index, scope.row)"  :disabled="scope.row.state==1">审核</el-button>
                    <el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="店铺名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="公司电话" prop="tel">
                    <el-input v-model="addForm.tel" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="注册时间" prop="registerTime">
                    <el-input v-model="addForm.registerTime" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="state">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="0">待审核</el-radio>
                    </el-radio-group>
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">审核通过</el-radio>
                    </el-radio-group>
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="-1">驳回</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="addForm.address" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="标志" prop="logo">
                    <el-input v-model="addForm.logo" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="管理员">
                    <el-select v-model="addForm.admin" value-key="id" filterable clearable placeholder="请选择">
                        <el-option
                                v-for="emp in employees"
                                :key="emp.id"
                                :label="emp.username"
                                :value="emp">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>


        <!--审核-->
        <el-dialog title="审核" :visible.sync="FormVisible" :close-on-click-modal="false">
            <el-form :model="dataForm" label-width="80px">
                <el-form-item label="店铺名称" prop="name">
                    <el-input v-model="dataForm.name" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="name">
                    <el-input v-model="dataForm.tel" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="logo" prop="name">
                    <el-input v-model="dataForm.logo" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>

                <el-form-item label="驳回原因">
                    <el-input type="textarea" v-model="dataForm.reason" ></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="FormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="pass" :loading="addLoading">通过</el-button>
                <el-button type="primary" @click.native="reject" :loading="addLoading">驳回</el-button>
            </div>
        </el-dialog>






    </section>
</template>

<script>
    export default {
        data(){
            return{
                listLoading:false, // 开启加载效果
                listData:[],
                sels:[],
                total:0, // 数据的总条数
                query:{
                    pageSize:5,
                    currentPage:1,
                    keywords:''
                },
                addFormVisible:false, // 默认隐藏弹出框
                FormVisible:false,// 审核弹出框
                addForm:{ // 数据双向绑定
                    name:'',
                    tel:'',
                    registerTime:'',
                    state:1,
                    address:'',
                    logo:'',
                    admin_id:'',
                    admin:null
                },
                dataForm:{
                    reason:""

                },
                addFormRules:{

                },
                addLoading:false, // 转圈圈
                employees:[],// 下拉框数据源

            }
        },
        methods:{
            pass(){
                let param = {
                    shop_id:this.dataForm.id,

                }
                this.$http.post("/shop/pass",param).then(res=>{

                    let {success,message}=res.data;
                    if (success){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else {
                        this.$message.error('网络异常');
                    }

                    this.FormVisible=false;

                    //重新加载数据
                    this.loadListData();


                })

            },



            // 和编辑一样，需要数据展示
            check(index,row){
                // 数据拷贝
                this.dataForm=Object.assign({},row);
                // 打开审核弹出框
                this.FormVisible=true;

            },
            // 驳回方法
                reject(){
                    // 判断取反如果什么都没输入,没有数据
                    if(!this.dataForm.reason){
                        // 提示输入驳回的原因
                        this.$message.error('错了哦，需要填写具体原因');
                        return;
                    }
                    // 组装请求参数
                    let param = {
                        shop_id:this.dataForm.id,
                        note:this.dataForm.reason
                    }
                    this.$http.post("/shop/reject",param).then(res=>{

                        let {success,message}=res.data;
                        if (success){
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                        }else {
                            this.$message.error('网络异常');
                        }
                        this.formVisible=false;
                        console.debug(this.formVisible);
                        //重新加载数据
                        this.loadListData();


                    })


            },

            selsChange(val){ // val是用户勾选的数据
                this.sels=val;
            },
            loadListData(){
                this.$http.post("/shop",this.query).then(res=>{
                    this.listData=res.data.data;
                    this.total=res.data.total;
                })
            },
            // 查询的方法
            search(){
                this.query.currentPage=1; // 初始化开始的页码
                this.loadListData(); // 重新加载页面
            },
            // 新增的方法
            handleAdd(){
                this.addForm={
                    name:'',
                    tel:'',
                    registerTime:'',
                    state:1,
                    address:'',
                    logo:'',
                    admin_id:'',
                    manager:null
                }
                this.addFormVisible=true;
            },

            // 编辑的方法
            handleEdit(index,row){
                // 数据拷贝
                this.addForm=Object.assign({},row);
                // 打开弹出框
                this.addFormVisible=true;
            },
            // 删除的方法
            handleDel(index,row){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => { // 当前用户选择确定的回调方法
                    // 发送请求，删除数据
                    this.$http.delete("/shop/"+row.id).then(res=>{
                        let{success,message}=res.data; // 结构表达式
                        if (success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }else{
                            this.$message.error("网络异常");
                        }
                    })
                    // 重新加载页面
                    this.loadListData();
                    this.formVisible=false;
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 批量删除
            batchRemove(){
                var ids = this.sels.map(item => item.id) // 将数组sels的数据item的id取出组成新的数组返回给ids
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 发送请求，删除数据
                    this.$http.patch("/shop/",ids).then(res=>{
                        let{success,message}=res.data; // 解构表达式
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }else{
                            this.$message.error("网路异常");
                        }
                    })
                    this.loadListData();
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 点击分页触发的方法
            handleCurrentChange(val){
                this.query.currentPage=val;
                this.loadListData();
            },
            // 提交
            addSubmit(){
                this.addFormVisible=true;
                this.$http.put("/shop",this.addForm).then(res=>{
                    let{success,message}=res.data; // 解构表达式
                    if(success){
                        this.$message({
                            type: 'success',
                            message: '添加成功!'
                        });
                    }else{
                        this.$message({
                            message:'操作失败'
                        });
                    }

                    this.addFormVisible=false;
                    this.loadListData();
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '操作失败删除'
                    });
                });
            },
            loadEmp(){ // 用来请求负责人的下拉框数据
                this.$http("/emp").then(res=>{
                    this.employees=res.data;
                })
            },

        },
        mounted(){
            this.loadListData();
            this.loadEmp();
        }
    }
</script>

<style>

</style>